#include "cell.h"
#include <iostream>
#include <sstream>
Cell::Cell()
{
}

Cell::Cell(int x, int y)
{
    this->x=x;
    this->y=y;
    this->grantedFor=NULL;
}

Cell::Cell(int x, int y, std::list<Robot*> robots, Robot* grantedFor){

    this->x=x;
    this->y=y;
    this->robotList=robots;
    this->grantedFor=grantedFor;
}
Cell::Cell(Cell* cell){
    this->x=cell->x;
    this->y=cell->y;
    this->addToRobotPath(cell->grantedFor);
}

void Cell::printCell(){
    std::cout<<this->x<<this->y;
    if (this->robotList.size())
        std::cout<<"r";this->printRobots();

    if(this->isGranted()){
        std::cout<<"g";
        std::cout<<this->getGrantedRobot()->getId();
    }
}


bool Cell::isPrivate(){
    int counter=0;
    std::list<Robot*> robots = robotList;
    robots.unique();
    std::list<Robot*>::iterator robotIt = robots.begin();
    for(;robotIt!=robots.end();++robotIt){
        if ((*robotIt)->isOnStage())
            ++counter;
    }
    if (counter==1)
        return true;
    else
        return false;
}

bool Cell::isOnPathOf(Robot *robot){
    std::list<Robot*>::iterator robotIt = robotList.begin();
    for(;robotIt!=robotList.end();++robotIt){
        if ((*robotIt)->getId()==robot->getId())
            return true;
    }
    return false;
}
void Cell::printRobots(){
    std::list<Robot*> robots = robotList;
    //robots.unique();
    std::list<Robot*>::iterator robotIt = robots.begin();
    for(;robotIt!=robots.end();++robotIt){
        std::cout<<(*robotIt)->getId();
    }
}

void Cell::removeGrant(){
    grantedFor=NULL;
}


int Cell::removeFromRobotPath(Robot* robot){


    //this->robotList.remove(robot);
    std::list<Robot*>::iterator rIt;
    for(rIt = robotList.begin();rIt!=robotList.end();++rIt){
        if ((*rIt)->getId()==robot->getId()){
            robotList.remove((*rIt)->getRobot());
            break;
        }
        std::cout<<"r"<<(*rIt)->getId()<<" removed from ";this->printCell();
    }
    //  this->printCell();
    //  std::cout<<"has been removed from path of robot"<<robot->getId()<<std::endl;
    //  std::cout<<"Cell's robot list size is"<<this->robotList.size()<<std::endl;
    int counter=0;
    std::list<Robot*>::iterator robotIt = robotList.begin();
    for(;robotIt!=robotList.end();++robotIt){
        if ((*robotIt)->isOnStage())
            ++counter;
    }
    return counter;
}


int Cell::removeRobot(Robot* robot){


    //this->robotList.remove(robot);
    std::list<Robot*>::iterator rIt;
    for(rIt = robotList.begin();rIt!=robotList.end();++rIt){
        if ((*rIt)->getId()==robot->getId()){
            robotList.erase(rIt);
            break;
        }
        std::cout<<"r"<<(*rIt)->getId()<<" removed from ";this->printCell();
    }
    int counter=0;
    std::list<Robot*>::iterator robotIt = robotList.begin();
    for(;robotIt!=robotList.end();++robotIt){
        if ((*robotIt)->isOnStage())
            ++counter;
    }
    return counter;
}

void Cell::addToRobotPath(Robot *robot){
    this->robotList.push_back(robot);
    //this->robotList.unique();
}
